#include "mouseevent.h"

MouseEvent::MouseEvent(QWidget *parent)
    :QWidget(parent)
{
    setTitleBarHidden(true);
    setMouseTracking(true);
}

void MouseEvent::setTitleBarHidden(bool isHidden){

    if(isHidden){
        this->setWindowFlags(Qt::FramelessWindowHint);
    }else{
        this->setWindowFlags(Qt::Window);
    }

}

void MouseEvent::mouseMoveEvent(QMouseEvent *event)
{
    QPoint pos = event->pos();
    // 如果不是在拖动状态，更新光标形状
    if (!m_isDragging) {
        Edge edge = getEdge(pos);
        //setCursorShape(edge);
        event->accept();
        return;
    }
    // 处理拖动调整大小
    if (m_isDragging && m_dragEdge != EdgeNone) {
        QPoint globalPos = event->globalPos();
        QPoint delta = globalPos - m_dragPosition;
        QRect geom = geometry();
        switch (m_dragEdge) {
        case EdgeTop:
            geom.adjust(0, delta.y(), 0, 0);
            break;
        case EdgeBottom:
            geom.adjust(0, 0, 0, delta.y());
            break;
        case EdgeLeft:
            geom.adjust(delta.x(), 0, 0, 0);
            break;
        case EdgeRight:
            geom.adjust(0, 0, delta.x(), 0);
            break;
        case EdgeTopLeft:
            geom.adjust(delta.x(), delta.y(), 0, 0);
            break;
        case EdgeTopRight:
            geom.adjust(0, delta.y(), delta.x(), 0);
            break;
        case EdgeBottomLeft:
            geom.adjust(delta.x(), 0, 0, delta.y());
            break;
        case EdgeBottomRight:
            geom.adjust(0, 0, delta.x(), delta.y());
            break;
        default:
            break;
        }
        // 确保调整后的大小不小于最小大小
        if (geom.width() >= minimumWidth() && geom.height() >= minimumHeight()) {
            setGeometry(geom);
            m_dragPosition = globalPos;
        }
        event->accept();
    }
    //获取鼠标在整个屏幕中的位置，
    //Qt5：用event->globalpos（）。
    //屏幕坐上角的居中坐标用 ：frameGeometry().topLeft();
    //计算差值：鼠标的位置-窗口的位置，保证拖动的时候，鼠标的位置与窗口的相对位置不变
    if (m_isDragging && m_dragEdge == EdgeNone) {
        move(event->globalPos() - m_dragPosition + frameGeometry().topLeft());
        m_dragPosition = event->globalPos();
        event->accept();
        return;
    }
}

void MouseEvent::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        m_dragPosition = event->globalPos();
        m_isDragging = true;

        // 确定拖动的是哪个边缘
        QPoint pos = event->pos();
        m_dragEdge = getEdge(pos);

        event->accept();
    }
}

void MouseEvent::mouseReleaseEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        m_isDragging = false;
        event->accept();
        return;
    }
}


void MouseEvent::closeEvent(QCloseEvent *event)
{
    QMessageBox msgbox(this);
    msgbox.setStandardButtons(QMessageBox::Yes |QMessageBox::Cancel );
    msgbox.setDefaultButton(QMessageBox::Cancel);
    msgbox.setText("是否退出程序？");
    msgbox.setWindowTitle("Tips");
    QPoint centerPos = this->geometry().center() - msgbox.rect().center();
    msgbox.move(centerPos);

    switch (msgbox.exec()) {
    case QMessageBox::Yes:
        event->accept();
        break;
    case QMessageBox::Cancel:
        event->ignore();
        break;
    default:
        break;
    }
}

MouseEvent::Edge MouseEvent::getEdge(const QPoint &pos) const
{
    int x = pos.x();
    int y = pos.y();
    int w = width();
    int h = height();

    bool top = (y <= EDGE_MARGIN);
    bool bottom = (y >= h - EDGE_MARGIN);
    bool left = (x <= EDGE_MARGIN);
    bool right = (x >= w - EDGE_MARGIN);

    if (top && left) return EdgeTopLeft;
    if (top && right) return EdgeTopRight;
    if (bottom && left) return EdgeBottomLeft;
    if (bottom && right) return EdgeBottomRight;
    if (top) return EdgeTop;
    if (bottom) return EdgeBottom;
    if (left) return EdgeLeft;
    if (right) return EdgeRight;
    return EdgeNone;
}

void MouseEvent::setCursorShape(MouseEvent::Edge edge)
{
    switch (edge) {
    case EdgeTop:
    case EdgeBottom:
        setCursor(Qt::SizeVerCursor);
        break;
    case EdgeLeft:
    case EdgeRight:
        setCursor(Qt::SizeHorCursor);
        break;
    case EdgeTopLeft:
    case EdgeBottomRight:
        setCursor(Qt::SizeFDiagCursor);
        break;
    case EdgeTopRight:
    case EdgeBottomLeft:
        setCursor(Qt::SizeBDiagCursor);
        break;
    case EdgeNone:
    default:
        setCursor(Qt::ArrowCursor);
        break;
    }
}


